<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="550px" @confirm="handleSubmit" @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="120px" :rules="formRules">
                <el-form-item :label="$t('语言名称')" prop="language_name">
                    <div class="flex-1">
                        <el-input v-model="formData.language_name" :placeholder="$t('请输入语言名称')" clearable />
                    </div>
                </el-form-item>
                <el-form-item :label="$t('语言标识')" prop="file_name">
                    <el-select class="flex-1" v-model="formData.file_name" clearable filterable
                        :placeholder="$t('请选择语言标识')">
                        <el-option v-for="(item, index) in optionsData.country" :key="index" :label="item.label"
                            :value="item.value" />
                    </el-select>
                </el-form-item>
                <el-form-item :label="$t('状态')" prop="status">
                    <el-radio-group v-model="formData.status">
                        <el-radio :label="0">{{ $t('关闭') }}</el-radio>
                        <el-radio :label="1">{{ $t('开启') }}</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item :label="$t('默认')" prop="is_default">
                    <el-radio-group v-model="formData.is_default">
                        <el-radio :label="0">{{ $t('关闭') }}</el-radio>
                        <el-radio :label="1">{{ $t('开启') }}</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import { useI18n } from 'vue-i18n'
const { t } = useI18n()
import { langEdit, langAdd, langDetail, langCountryAll } from '@/api/setting/lang'
import Popup from '@/components/popup/index.vue'
import { useDictOptions } from '@/hooks/useDictOptions'
import useLangStore from '@/stores/modules/lang'
const langStore = useLangStore()
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
    return mode.value == 'edit' ? t('编辑语言') : t('新增语言')
})

const formData = reactive({
    id: '',
    language_name: '',
    file_name: '',
    status: 0,
    is_default: 0,
})

const formRules = {
    language_name: [
        {
            required: true,
            message: t('请输入语言名称'),
            trigger: ['blur']
        }
    ],
    file_name: [
        {
            required: true,
            message: t('请选择语言标识'),
            trigger: ['blur']
        }
    ],
}

const { optionsData } = useDictOptions<{
    country: any[]
}>({
    country: {
        api: langCountryAll
    }
})

const handleSubmit = async () => {
    await formRef.value?.validate()
    mode.value == 'edit' ? await langEdit(formData) : await langAdd(formData)
    await langStore.getLangConfig()
    popupRef.value?.close()
    emit('success')
}

const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

const setFormData = (data: Record<any, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
}

const getDetail = async (row: Record<string, any>) => {
    const data = await langDetail({
        id: row.id
    })
    setFormData(data)
}

const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
